<?

include "lib_config.php";
include "constants.php";
include "model.php";
include "controller.php";

//no SQL injection!
function sanatizeGet($value) {
	$output = mysql_real_escape_string($value);
	return $output;
}

///////////////////////////////////////
// DIRTY OLD CODE LEFT OVER FROM V2. //
///////////////////////////////////////

//return what teams played in, and who won, a given set of elim matches
function whowon($eventid, $complevel, $setnumber) {
	global $complevelnames, $complevelshort, $weeknames;
	
	$setlow = $setnumber * 10 - 1;
	$sethigh = ($setnumber + 1) * 10;
	
	//look up what event we're dealing with
	$matchinfo_query_filter = "SELECT * FROM `matches` WHERE `event`='$eventid' AND `complevel`='$complevel' AND `matchnumber` > $setlow AND `matchnumber` < $sethigh ORDER BY `matchnumber` DESC LIMIT 1";
	$matchinfo_query = mysql_query($matchinfo_query_filter);
	$matchinfo = mysql_fetch_array($matchinfo_query);
	
	$returnvalue["red1"] = $matchinfo["red1"];
	$returnvalue["red2"] = $matchinfo["red2"];
	$returnvalue["red3"] = $matchinfo["red3"];
	$returnvalue["blue1"] = $matchinfo["blue1"];
	$returnvalue["blue2"] = $matchinfo["blue2"];
	$returnvalue["blue3"] = $matchinfo["blue3"];
	
	if ($matchinfo["redscore"] > $matchinfo["bluescore"]) {
		$returnvalue["winner"] = "red";
	} elseif ($matchinfo["redscore"] < $matchinfo["bluescore"]) {
		$returnvalue["winner"] = "blue";
	} else {
		$returnvalue["winner"] = "tie";
	}	
	
	return $returnvalue;
}

//make whowon look pretty.
function formatwhowon($whowon, $color, $rowspan, $LR) {
    $tba = new tbatvController();
	//abort if no info.
	if (isset($whowon["red1"])) {
		if ($color == $whowon["winner"]) $winnerclass = " winner" . $LR;
		if ($color != $whowon["winner"] AND $LR == "C") $winnerclass = " secondC";
		echo "<td class=\"{$color}{$winnerclass}\" rowspan=\"$rowspan\">";
		
		if ($color == $whowon["winner"]) echo "<strong>";
		if ($color == "red") echo $tba->linkTeam($whowon["red1"]) . "<br />" . $tba->linkTeam($whowon["red2"]) . "<br />" . $tba->linkTeam($whowon["red3"]);
		if ($color == "blue") echo $tba->linkTeam($whowon["blue1"]) . "<br />" . $tba->linkTeam($whowon["blue2"]) . "<br />" . $tba->linkTeam($whowon["blue3"]);
		if ($color == $whowon["winner"]) echo "</strong>";
		
		echo "</td>";
	}
}

//generate the whole elims bracket.
function generateElimsBracket($eventid) {
	?>
	<table class="brackettable" cellspacing="0">
	<tr><td colspan="6" class="header">Eliminations Bracket</td></tr>
	
	<tr><? formatwhowon(whowon($eventid, 30, 1), "red", 1, "L") ?><? formatwhowon(whowon($eventid, 40, 1), "red", 2, "L") ?><? formatwhowon(whowon($eventid, 50, 1), "red", 4,"C") ?><? formatwhowon(whowon($eventid, 50, 1), "blue",4,"C") ?><? formatwhowon(whowon($eventid, 40, 2), "blue",2,"R") ?><? formatwhowon(whowon($eventid, 30, 4), "red", 1,"R") ?></tr>
	
	<tr><? formatwhowon(whowon($eventid, 30, 1), "blue", 1,"L") ?><? formatwhowon(whowon($eventid, 30, 4), "blue", 1,"R") ?></tr>
	
	<tr><? formatwhowon(whowon($eventid, 30, 2), "red", 1,"L") ?><? formatwhowon(whowon($eventid, 40, 1), "blue", 2,"L") ?><? formatwhowon(whowon($eventid, 40, 2), "red", 2,"R") ?><? formatwhowon(whowon($eventid, 30, 3), "red", 1,"R") ?></tr>
	
	<tr><? formatwhowon(whowon($eventid, 30, 2), "blue", 1,"L") ?><? formatwhowon(whowon($eventid, 30, 3), "blue", 1,"R") ?></td></tr>
	</table>
	<?
}

?>